/*
 * Project: Sudoku Solver
 * File: solve.c
 *
 * Copyright (C) 2009 Daniel Meekins
 * Contact: dmeekins - gmail
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "cell.h"
#include "grid.h"
#include "solve.h"

#define MAX_ITERATIONS 50

/* Internal function prototypes */
void solve_elim_in_grid(grid_t *g);
void solve_find_loner(grid_t *g);

/*
 * The main solver function. Runs a loop calling each of the tests according
 * to the passed difficulty level until the grid is solved or it went through
 * the maximum number of iterations through the puzzle.
 */
int solve(uint32_t puzzle[ROWS][COLS], int difficulty)
{
    int i;
    grid_t *g;
    
    if((g = grid_new(puzzle)) == NULL)
    {
        fprintf(stderr, "Error making a new grid\n");
        return -1;
    }
    
    grid_print(g);
    
    for(i = 0; i < MAX_ITERATIONS && !grid_issolved(g); i++)
    {
        //system("clear"); /* viewer */
        //grid_print(g); /* viewr */
        //sleep(1); /* viewer */

        //printf("iteration num: %d\n", i);
        //grid_print_full(g);
        
        switch(difficulty)
        {
            case DIFFICULTY_VERY_HARD:
                /* fall through */
                
            case DIFFICULTY_HARD:
                /* fall through */
                
            case DIFFICULTY_MEDIUM:
                solve_find_loner(g);
                GRID_CHECK(g);
                /* fall through */
                
            case DIFFICULTY_EASY:
                solve_elim_in_grid(g);
                GRID_CHECK(g);
                /* fall through */
                
            default:
                grid_update(g);
                GRID_CHECK(g);
        }
        
        GRID_CHECK(g);
    }
    
    //system("clear"); /* viewer */
        
    printf("\n");
    grid_print(g);
    printf("\n");

    grid_free(g);
    
    return 0;
}

/*
 * Goes through the rows, columns, and subgrids to find a cell with a number as
 * the only possiblilty in a row, column, or subgrid.
 */
void solve_find_loner(grid_t *g)
{
    cell_t *cell;
    cell_t *poss_cell;
    uint32_t r, c, i, j, gr, gc;
    cell_t tmp;
    
    poss_cell = &tmp;
    
    /* go through each row */
    for(r = 0; r < ROWS; r++)
    {
        for(c = 0; c < COLS; c++)
        {
            cell = grid_cell_at(g, r, c);
            
            if(cell_issolved(cell))
                continue;
            
            cell_clear(poss_cell);
            
            for(j = 0; j < COLS; j++)
                if(c != j)
                    cell_union(poss_cell, poss_cell, grid_cell_at(g, r, j));
            
            cell_invert(poss_cell);
            
            if(cell_possible_count(poss_cell) == 1)
            {
                cell_copy(cell, poss_cell);
                GRID_CHECK(g);
                return;
            }
        }
    }
    GRID_CHECK(g);
    
    /* go through each column */
    for(c = 0; c < COLS; c++)
    {
        for(r = 0; r < ROWS; r++)
        {
            cell = grid_cell_at(g, r, c);
            
            if(cell_issolved(cell))
                continue;
            
            cell_clear(poss_cell);
            
            for(i = 0; i < ROWS; i++)
                if(r != i)
                    cell_union(poss_cell, poss_cell, grid_cell_at(g, i, c));
            
            cell_invert(poss_cell);
            
            if(cell_possible_count(poss_cell) == 1)
            {
                cell_copy(cell, poss_cell);
                GRID_CHECK(g);
                return;
            }
        }
    }
    GRID_CHECK(g);
    
    /* go through each subgrid */
    for(gr = 0; gr < G_ROWS; gr++)
    {
        for(gc = 0; gc < G_COLS; gc++)
        {
            /* for each cell in the subgrid */
            for(r = 0; r < ROWS / G_ROWS; r++)
            {
                for(c = 0; c < COLS / G_COLS; c++)
                {
                    cell = grid_local_cell_at(g, gr, gc, r, c);
                    
                    if(cell_issolved(cell))
                        continue;

                    cell_clear(poss_cell);
                    
                    for(i = 0; i < ROWS / G_ROWS; i++)
                        for(j = 0; j < COLS / G_COLS; j++)
                            if(i != r || j != c)
                                cell_union(poss_cell, poss_cell,
                                          grid_local_cell_at(g, gr, gc, i, j));
                    
                    cell_invert(poss_cell);

                    if(cell_possible_count(poss_cell) == 1)
                    {
                        cell_copy(cell, poss_cell);
                        GRID_CHECK(g);
                        return;
                    }
                }
            }
        }
    }    
    GRID_CHECK(g);
}

/*
 * Goes through each subgrid and eliminates the already solved numbers from
 * the possible numbers for each unsolved cell.
 */
void solve_elim_in_grid(grid_t *g)
{
    cell_t *cell;
    uint32_t gr, gc, r, c, i;
    uint32_t num_count[CELL_MAX_NUM];
    
    /* loop through each subgrid */
    for(gr = 0; gr < G_ROWS; gr++)
    {
        for(gc = 0; gc < G_COLS; gc++)
        {
            memset(num_count, 0, sizeof(num_count));
            
            /* loop through each cell in the subgrid and count the number of
             * of possibilities for each number */
            for(r = 0; r < ROWS / G_ROWS; r++)
            {
                for(c = 0; c < COLS / G_COLS; c++)
                {
                    cell = grid_local_cell_at(g, gr, gc, r, c);
                    
                    if(!cell_issolved(cell))
                        cell_fill_possible_num_count(cell, num_count);
                }
            }
            
            /* go through the count array to see if any numbers have only a
               count of one, which means there is a cell that can only be
               that number */
            for(i = 0; i < CELL_MAX_NUM; i++)
            {
                if(num_count[i] == 1)
                {
                    /* find the cell that can be this number and set it */
                    for(r = 0; r < ROWS / G_ROWS; r++)
                    {
                        for(c = 0; c < COLS / G_COLS; c++)
                        {
                            if(cell_num_ispossible(
                                   grid_local_cell_at(g, gr, gc, r, c), i+1))
                            {
                                grid_set_local_cell_num(g, i+1, gr, gc, r, c);
                                
                                /* break out of local for loops */
                                c = COLS / G_COLS;
                                r = ROWS / G_ROWS;
                            }
                        }
                    }
                }
            }
        }
    }
}
